//
//  PSPublisher.m
//  PubSub
//
//  Created by Pieter Omvlee on 6/27/08.
//  Copyright 2008 Bohemian Coding. All rights reserved.
//

#import "CHPublisher.h"


@implementation CHPublisher

//setup & teardown

- (id) init
{
  self = [super init];
  if (self != nil) {
    subscribers = [[NSMutableArray alloc] init];
  }
  return self;
}

- (void) dealloc
{
  [subscribers release];
  [super dealloc];
}

//key-methods

- (void)publish:(id)anObject
{
  for (id<CHSubscriber> subscriber in subscribers)
    [subscriber publisher:self publishedObject:anObject];
}

//operations

- (void)addSubscriber:(id<CHSubscriber>)aSubscriber
{
  [subscribers addObject:aSubscriber];
}

- (void)removeSubscriber:(id<CHSubscriber>)aSubscriber
{
  [subscribers removeObject:aSubscriber];
}

- (void)removeAllSubscribers
{
  [subscribers removeAllObjects];
}

//overwriten

- (id)copyWithZone:(NSZone *)zone
{
  id result = [[[self class] allocWithZone:zone] init];
  [result setSubscribers:[[subscribers copy] autorelease]];
  
  return result;
}

- (void)encodeWithCoder:(NSCoder *)coder
{
  [coder encodeObject:subscribers forKey:@"subscribers"];
}

- (id)initWithCoder:(NSCoder *)decoder
{
  self = [super init];
  if (self)
    self.subscribers = [decoder decodeObjectForKey:@"subscribers"];
  
  return self;
}

//properties

@synthesize subscribers;

@end
